package com.mxpio.erp.plm.monitoring;

import com.mxpio.erp.common.plm.entity.*;
import com.mxpio.erp.common.plm.service.*;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.message.service.MessageService;
import com.mxpioframework.security.util.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 审批阶段的站内信
 */
@Slf4j
@Component
public class ProjectApprovalMessage implements JavaDelegate {

    public static final String[] channelCodes = {"innerMsg"};

    @Resource
    private MessageService messageService;//消息
    @Autowired
    private ProjectInfoService projectInfoService;
    @Autowired
    private ProjectTaskService projectTaskService;
    @Autowired
    private ProjectInfoApprovalService projectInfoApprovalService;
    @Autowired
    private ProjectInfoChangeService projectInfoChangeService;
    @Autowired
    private ProjectInfoCompleteService projectInfoCompleteService;
    @Autowired
    private ProjectInfoEndService projectInfoEndService;
    @Autowired
    private ProjectTaskExecutionLogService projectTaskExecutionLogService;





    @Override
    @Transactional
    public void execute(DelegateExecution delegateExecution) throws Exception {
        String processInstanceId = delegateExecution.getProcessInstanceId();
        //判断是哪个审批流程
        //立项
        ProjectInfoApproval approval = JpaUtil.linq(ProjectInfoApproval.class).equal("processInstanceId", processInstanceId).findOne();
        //变更
        ProjectInfoChange change = JpaUtil.linq(ProjectInfoChange.class).equal("processInstanceId", processInstanceId).findOne();
        //结项
        ProjectInfoComplete complete = JpaUtil.linq(ProjectInfoComplete.class).equal("processInstanceId", processInstanceId).findOne();
        //终止
        ProjectInfoEnd end = JpaUtil.linq(ProjectInfoEnd.class).equal("processInstanceId", processInstanceId).findOne();
        //任务完工
        ProjectTaskExecutionLog taskExecutionLog = JpaUtil.linq(ProjectTaskExecutionLog.class).equal("processInstanceId", processInstanceId).findOne();

        //判断是哪个审批流程
        if (approval != null){
            String[] toUsers = {approval.getApplicant()};
            //发送站内信
            messageService.sendMessage(channelCodes,SecurityUtils.getLoginUsername(),toUsers,"工作流审核任务-结束提醒","您的审核任务已结束，请注意查看");
        }else if (change != null){
            String[] toUsers = {change.getApplicant()};
            //发送站内信
            messageService.sendMessage(channelCodes,SecurityUtils.getLoginUsername(),toUsers,"工作流审核任务-结束提醒","您的审核任务已结束，请注意查看");

        } else if (complete != null){
            String[] toUsers = {complete.getApplicant()};
            //发送站内信
            messageService.sendMessage(channelCodes,SecurityUtils.getLoginUsername(),toUsers,"工作流审核任务-结束提醒","您的审核任务已结束，请注意查看");

        } else if (end != null){
            String[] toUsers = {end.getApplicant()};
            //发送站内信
            messageService.sendMessage(channelCodes,SecurityUtils.getLoginUsername(),toUsers,"工作流审核任务-结束提醒","您的审核任务已结束，请注意查看");

        }else if (taskExecutionLog != null) {
            String[] toUsers = {taskExecutionLog.getApplicant()};
            //发送站内信
            messageService.sendMessage(channelCodes,SecurityUtils.getLoginUsername(),toUsers,"工作流审核任务-结束提醒","您的审核任务已结束，请注意查看");

        }
    }
}
